package org.thesis.secutity;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;
import android.util.Log;

public class AESControl 
{
	Cipher Cip;
    SecretKeySpec KeySpecAES;
    FileInputStream fis;
    FileOutputStream fos;
    
    String tag = "AEScontrol";
    

    public AESControl(SecretKeySpec keySpecAES) {
		super();
		KeySpecAES = keySpecAES;
	}


    public String Encrypt(String input)
    {
        try
        {
           // JOptionPane.showMessageDialog(null, KeySpecAES.toString());
            Cip = Cipher.getInstance("AES");
            Cip.init(Cipher.ENCRYPT_MODE, KeySpecAES); // ma hoa su dung keySpec
            byte[] utf8 = input.getBytes("UTF8");
            byte[] enc = Cip.doFinal(utf8);
            return Base64.encodeToString(enc, 0);
//            return new sun.misc.BASE64Encoder().encode(enc); // chuyen sang dang base64
        }
        catch(Exception e)
        {
        	 Log.v(tag, "Loi Encypt AES "+e.toString());
            return null;
        }
    }
    public String Decrypt(String input)
    {
        try
        {
            Cip = Cipher.getInstance("AES");
            Cip.init(Cipher.DECRYPT_MODE, KeySpecAES); // ma hoa su dung keySpec
//            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(input); // chuyen base 64 sang byte
            byte[] dec = Base64.decode(input, 0);
            // Decrypt
            byte[] utf8 = Cip.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        }
        catch(Exception e)
        {
        	 Log.v(tag,"Loi Decrypt AES "+e.toString());
            return null;
        }
    }

    public void EncFile (String input, String output)
    {
        try
        {
            Cip = Cipher.getInstance("AES");
            Cip.init(Cipher.ENCRYPT_MODE, KeySpecAES);
            File fi = new File(input);
            File fo = new File(output);
            fis = new FileInputStream(fi);
            CipherOutputStream cos = new CipherOutputStream(new FileOutputStream(fo), Cip);
            copy(fis, cos);
            cos.close();
            fis.close();

        }
        catch(Exception e)
        {
        	 Log.v(tag, "Loi ma hoa file AES" + e.toString());
        }
    }
    public void DecFile(String input, String output)
    {
        try
        {
            Cip = Cipher.getInstance("AES");
            Cip.init(Cipher.DECRYPT_MODE, KeySpecAES);
            CipherInputStream cis = new CipherInputStream(new FileInputStream(input), Cip);
            fos = new FileOutputStream(output);
            copy(cis, fos);
            cis.close();
            fos.close();
        }
        catch(Exception e)
        {
            Log.v(tag, "Loi giai ma file AES" + e.toString());
        }
    }
     private void copy(InputStream is, OutputStream os) throws IOException {
		int i;
		byte[] b = new byte[1024];
		while((i=is.read(b))!=-1) {
			os.write(b, 0, i);
		}
	}

}
